package com.iamk.feature;

import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.iamk.segment.SRMSegment;
import com.iamk.util.GetData;

public class FColor {
	final static int NUMBER_SEG = 3;
	int height;
	int width;
	BufferedImage img;
	public Image imgSeg;
	public int[][] mask;
	String fileMask;
	int maxRegion;
	int minPixcel;
	public Region[] regions;
	public List<String> listLabel;
	// Image segmentation
	SRMSegment srm;

	public FColor(BufferedImage img, String name, String fileMask) {
		this.img = img;
		this.fileMask = fileMask;
		this.listLabel = new ArrayList<String>();
		if(fileMask.equalsIgnoreCase("")){
			srm = new SRMSegment(img, name);
			this.mask = srm.mask;
			this.imgSeg = srm.imgseg;
		}else {
			getRegion();
		}
		minPixcel = 0;
	}

	public FColor(BufferedImage img, String fileMask, int minPixel) {
		// TODO Auto-generated constructor stub
		this.img = img;
		this.fileMask = fileMask;
		this.minPixcel = minPixel;
	}

	private void init() {
		// TODO Auto-generated method stub
		maxRegion = 0;
		height = img.getHeight();
		width = img.getWidth();
		if (mask == null) {
			mask = GetData.readMaskFile(fileMask);
		}
		for (int i = 0; i < mask.length; i++)
			for (int j = 0; j < mask[0].length; j++) {
				if (maxRegion < mask[i][j]) {
					maxRegion = mask[i][j];
				}
			}
		// System.out.println(maxRegion);
	}

	public ArrayList<float[]> extract() {
		ArrayList<float[]> result = new ArrayList<float[]>();
		float[] hsvPixel = new float[3];
		init();
		Region[] regions = new Region[maxRegion];
		Color pixelColor = null;
		for (int i = 0; i < regions.length; i++) {
			regions[i] = new Region();
		}
		for (int x = 0; x < height; x++) {
			for (int y = 0; y < width; y++) {
				if (mask[x][y] > 0) {
					pixelColor = new Color(img.getRGB(y, x));
					// hsvPixel = new float[3];
					Color.RGBtoHSB(pixelColor.getRed(), pixelColor.getGreen(),
							pixelColor.getBlue(), hsvPixel);
					regions[mask[x][y] - 1].pixels.add(hsvPixel.clone());
				}
			}
		}
		for (int i = 0; i < regions.length; i++) {
			if (regions[i].pixels.size() > minPixcel) {
				regions[i].extract();
				result.add(regions[i].features);
			}
		}
		// System.out.println("Số region "+ regions.length);
		return result;
	}
	
	public void getRegion(){
		float[] hsvPixel = new float[3];
		init();
		regions = new Region[maxRegion];
		Color pixelColor = null;
		for (int i = 0; i < regions.length; i++) {
			regions[i] = new Region();
		}
		for (int x = 0; x < height; x++) {
			for (int y = 0; y < width; y++) {
				if (mask[x][y] > 0) {
					pixelColor = new Color(img.getRGB(y, x));
					// hsvPixel = new float[3];
					Color.RGBtoHSB(pixelColor.getRed(), pixelColor.getGreen(), pixelColor.getBlue(), hsvPixel);
					regions[mask[x][y] - 1].pixels.add(hsvPixel.clone());
				}
			}
		}
	}
	
	public void saveMask(String path, int[][] mask){
		// Save mask
		File file = new File(path);
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(file));
			for (int i = 0; i < mask[0].length; i++) {
				// for each row
				for (int j = 0; j < mask.length; j++) // for each column
				{
					int write = (j == (mask.length - 1)) ? 1 : 0;
					if (write == 1)
						bw.write(mask[j][i] + "");
					else
						bw.write(mask[j][i] + ",");
				}
				bw.write("\n");
			}
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private int[][] T(int[][] mask){
		int[][] maskT=new int[mask[0].length][mask.length];
		for(int i=0;i<mask.length;i++)
			for(int j=0;j<mask[0].length;j++){
			maskT[j][i]=mask[i][j];
		}
		return maskT;
	}
}
